package com.java.day08.homework.pk;

/**
 * 英雄类
 */
public class Hero implements Fightable {
    //名字
    private String name;

    //生命
    private int life;

    //伤害
    private int Attack;

    public Hero() {
    }

    public Hero(String name, int life, int attack) {
        this.name = name;
        this.life = life;
        Attack = attack;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getLife() {
        return life;
    }

    public void setLife(int life) {
        this.life = life;
    }

    public int getAttack() {
        return Attack;
    }

    public void setAttack(int attack) {
        Attack = attack;
    }

    @Override
    public String toString() {
        return '{'+
                "名字:'" + name + '\'' +
                ", 生命:" + life +
                ", 伤害:" + Attack
                +'}';
    }




    @Override
    //是否活着
    public boolean isAlive() {
        if (this.getLife()>0){
            return true;
        }
        return false;
    }

    @Override
    //攻击
    public void fight(Fightable enemy) {
        System.out.println(this.name+"发起攻击");
        enemy.takeDamage(this.getAttack());
    }

    @Override
    //受到伤害
    public void takeDamage(int damage) {
        System.out.println(this.name+"受到"+damage+"点伤害");
        this.life-=damage;
    }


    //重写equals
    @Override
    public boolean equals(Object obj) {
        if (obj==null){
            return false;
        }

        if(this==obj){
            return true;
        }
        if (obj instanceof Hero) {
            Hero anotherHero = (Hero) obj;
            if (this.getName().equals(anotherHero.getName()) &&
                    this.getLife() == anotherHero.getLife() && this.getAttack()== anotherHero.getAttack()) {
                return true;
            }
        }
        return false;
    }


}
